<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Autonym;
use App\Models\User;
use App\Models\UserAlipay;
use App\Models\UserWechat;
use App\Models\UsdtPrice;
use App\Models\Message;
use DB;
use Exception;
use Illuminate\Http\Request;

class CenterController extends Controller
{
    /**
     * 保存实名认证信息（弃用）
     *
     * @author hxq
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function save_real_info(Request $request)
    {
        try {
            //验证必传参数
            if (!$request->has('uid', 'real_name', 'user_card', 'front_photo', 'back_photo')) {
                //抛出错误
                throw new Exception(1003);
            }
            //用户id
            $uid = $request->get('uid');
            //真实姓名
            $real_name = $request->get('real_name');
            //身份证号码
            $user_card = $request->get('user_card');
            //正面照片
            $front_photo = $request->get('front_photo');
            //背面照片
            $back_photo = $request->get('back_photo');
            //查找用户
            $user = User::find($uid);
            //用户不存在
            if (!$user) {
                //抛出错误
                throw new Exception(1001);
            }
            //查找实名认证
            $check = Autonym::where(['uid' => $uid])->first();
            //信息已存在
            if ($check) {
                //抛出错误
                throw new Exception(1016);
            }
            //开启事务
            DB::beginTransaction();
            //写入实名表
            Autonym::create([
                'uid'         => $uid,
                'real_name'   => $real_name,
                'user_card'   => $user_card,
                'back_photo'  => $back_photo,
                'front_photo' => $front_photo,
                'status'      => 1,
            ]);
            $user->update([
                'real_status' => 3,
            ]);

            DB::commit();

            return response()->json(['status' => 1, 'msg' => '提交成功']);

        } catch (Exception $e) {
            //回滚
            DB::rollback();
            //接收错误号
            $msg = $e->getMessage();
            //返回
            return respF($msg);
        }
    }

    /**
     * 绑定支付宝
     *
     * @author hxq
     * @param  Request $request [description]
     * @return \Illuminate\Http\JsonResponse [type]           [description]
     */
    public function bind_alipay(Request $request)
    {
        //验证必传参数
        if (!$request->has('uid', 'alipay_account', 'alipay_code', 'pay_pass', 'real_name')) {
            //返回错误提示
            return respF(1000);
        }
        //用户id
        $uid = $request->get('uid');
        //支付宝账号
        $alipay_account = $request->get('alipay_account');
        //二维码图片
        $alipay_code = $request->get('alipay_code');
        //交易密码
        $pay_pass = $request->get('pay_pass');
        //真实姓名
        $real_name = $request->get('real_name');
        //验证参数
        if (empty($uid) || empty($alipay_account) || empty($alipay_code)
            || empty($pay_pass) || empty($real_name)) {
            //参数错误
            return respF(1003);
        }
        //查找用户
        $user = User::find($uid);
        //用户不存在
        if (!$user) {
            return respF(1001);
        }
        //验证交易密码
        if ($user->pay_pass != md5($pay_pass)) {
            //返回错误提示
            return respF(2000);
        }
        //插入数据库
        UserAlipay::updateOrCreate(
            [
                'uid' => $uid,
            ],
            [
                'alipay_account' => $alipay_account,
                'alipay_code'    => $alipay_code,
                'real_name'      => $real_name,
            ]
        );

        //消息推送
        $msgArr = ['msg_id' => 18, 'uid' => $uid, 'message' => ['支付宝']];
        (new Message())->syspublishsmsAll($msgArr);

        //成功提示
        return response()->json(['status' => 1, 'msg' => '保存成功']);
    }

    /**
     * 绑定微信
     *
     * @author hxq
     * @param  Request $request [description]
     * @return \Illuminate\Http\JsonResponse [type]           [description]
     */
    public function bind_wechat(Request $request)
    {
        //验证必传参数
        if (!$request->has('uid', 'wechat_account', 'wechat_code', 'pay_pass', 'real_name')) {
            //返回错误提示
            return respF(1000);
        }
        //用户id
        $uid = $request->get('uid');
        //微信账号
        $wechat_account = $request->get('wechat_account');
        //微信二维码图片
        $wechat_code = $request->get('wechat_code');
        //交易密码
        $pay_pass = $request->get('pay_pass');
        //真实姓名
        $real_name = $request->get('real_name');
        //验证参数
        if (empty($uid) || empty($wechat_account) || empty($wechat_code)
            || empty($pay_pass) || empty($real_name)) {
            //参数错误
            return respF(1003);
        }
        //查找用户
        $user = User::find($uid);
        //验证用户
        if (!$user) {
            //用户不存在
            return respF(1001);
        }
        //验证交易密码
        if ($user->pay_pass != md5($pay_pass)) {
            //交易密码不正确
            return respF(2000);
        }
        //插入数据库
        UserWechat::updateOrCreate(
            [
                'uid' => $uid,
            ],
            [
                'wechat_account' => $wechat_account,
                'wechat_code'    => $wechat_code,
                'real_name'      => $real_name,
            ]
        );

        //消息推送
        $msgArr = ['msg_id' => 18, 'uid' => $uid, 'message' => ['微信']];
        (new Message())->syspublishsmsAll($msgArr);

        //成功提示
        return response()->json(['status' => 1, 'msg' => '保存成功']);
    }

    /**
     * 获取后台设置的usdt买入卖出价
     * @author hxq
     * @return [type] [description]
     */
    public function rech_public_fit()
    {
        $data = [];
        //查找当前设置的usdt价格
        $price                = UsdtPrice::find(1);
        $data['buy_price']    = $price->buy_price ?? 0;
        $data['seller_price'] = $price->seller_price ?? 0;

        return response()->json(['status' => 1, 'msg' => '查询成功', 'data' => $data]);
    }
}
